from logging import getLogger
from multiprocessing import Lock
import os

import suds.metrics as metrics
from Config.RunConfig import RunConfig, Options

class Logger:

    log = getLogger('StressTest')
    timer = metrics.Timer()
    mutex = Lock()

    @staticmethod
    def logBegin(operation):
        if RunConfig().get(Options.StressTest.logger) == 'True':
            try:
                Logger.mutex.acquire()
                Logger.timer.start()
                Logger.log.info("[PID %s] Op '%s' is to be executed...", os.getpid(), operation.__name__)
            finally:
                Logger.mutex.release()


    @staticmethod
    def logEnd(operation, successfull):
        if RunConfig().get(Options.StressTest.logger) == 'True':
            try:
                Logger.mutex.acquire()
                Logger.timer.stop()

                slowly = ' '
                if Logger.timer.duration() > float(RunConfig().get(Options.StressTest.slowRequestDuration)):
                    slowly = ' slowly '

                withError = ' '
                if successfull == False:
                    withError = ' withError '

                Logger.log.info("[PID %s] Op '%s'" + slowly + "executed" + withError + "(%s).", os.getpid(), operation.__name__, Logger.timer)
            finally:
                Logger.mutex.release()
